package com.bkfm.framework.core.rest;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yak.common.digest.DESUtils;
import org.yak.core.web.BaseRest;
import org.yak.core.web.Result;
import org.yak.core.web.Result.Status;
import org.yak.core.web.ResultSet;

import com.bkfm.framework.core.api.service.IBkfmTableMetaServer;
import com.bkfm.framework.core.entity.BkfmFilter;
import com.bkfm.framework.core.entity.BkfmTableMetaData;

/**
 * 
 *
 * @平台：统一开发平台
 * @文件名称：BkfmDBRest.java
 * @文件描述：sql解析器
 * @author xuqiang
 * @日期：2018年6月25日下午3:49:21
 *
 */
@RestController
@RequestMapping("/bkfm/db/v1/**")
public class BkfmDBRest extends BaseRest<BkfmTableMetaData>{

	@Resource
	private IBkfmTableMetaServer tableMetaService;
	
	/**
	 * 执行查询sql结果返回json对象
	 * @param sql
	 * @return
	 */
	@RequestMapping("/query")
	public Result Query(String sql){
		try{
			List<ResultSet> rs = tableMetaService.queryResultBySql(DESUtils.decryption(sql), null);
			return new Result(Status.OK,rs);
		}catch(Exception e){
			logger.error("执行自定义sql异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 检查表记录数
	 * @param bkfmFilter
	 * @return
	 */
	@RequestMapping("/checknumber")
	public Result CheckNumber(BkfmFilter bkfmFilter) {
		try {
			int count = tableMetaService.checkNumber(bkfmFilter);
			return new Result(Status.OK,"",count);
		}catch(Exception e) {
			logger.error("查询业务模型记录数异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 执行更新sql
	 * @param sqllist
	 * @return
	 */
	@RequestMapping("/execute")
	public Result Execute(@RequestBody List<String> sqllist) {
		try{
			tableMetaService.executeBatchSql(sqllist, null);
			return new Result(Status.OK,"success");
		}catch(Exception e){
			logger.error("执行自定义sql异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
}
